package com.jp.knowledge.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jp.knowledge.R;
import com.jp.knowledge.model.Banner;
import com.jp.knowledge.util.ImageLoadUtil;

import org.xutils.common.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiayen on 16/11/3.
 * <p>
 * 自动滚动的 viewPager 针对图片
 */
public class CustomScrollViewPager extends AutoScrollViewPager {

    private DotLayout dotLayout;
    private List<View> list;
    private List<Banner> imgs;
    private PagerAdapter adapter;
    private Context mContext;
    private int width;
    private int heigth;

    private int lastPosition;//记录上一个位置

    private boolean maxScroll;

    public CustomScrollViewPager(Context context) {
        this(context,null);
    }

    public CustomScrollViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext=context;
        initData();
    }

    /**
     * 初始化数据
     */
    private void initData() {
        width = DensityUtil.getScreenWidth();
        heigth=DensityUtil.getScreenHeight()/4;
        adapter = new PagerAdapter();
        this.setAdapter(adapter);
        this.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(dotLayout != null){
                    dotLayout.getChildAt(lastPosition).setBackgroundResource(R.drawable.dot_com);
                    dotLayout.getChildAt(position).setBackgroundResource(R.drawable.dot_select);
                    lastPosition = position;
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //底部添加点提示
        addBottomLayout();
    }
    private void addBottomLayout() {

    }


    /**
     * 更新pager状态
     * @param imgs
     * @param dotLayout
     */
    public void update(List<Banner> imgs, DotLayout dotLayout){
        this.dotLayout = dotLayout;
        if(dotLayout != null){
            View firstView = dotLayout.getChildAt(0);
            if(firstView != null){
                firstView.setBackgroundResource(R.drawable.dot_select);
            }
        }
        this.imgs=imgs;
        initViews(imgs);
        adapter.notifyDataSetChanged();
        if(dotLayout != null)
            scroll();
    }

    private void scroll(){
        setInterval(2500);
        setAutoScrollDurationFactor(5);
        startAutoScroll();
    }


    /**
     * 初始化图片视图
     * @param imgs
     */
    private void initViews(List<Banner> imgs) {
        list = new ArrayList<>();
        for (Banner banner:imgs) {
            ImageView imgView = new ImageView(mContext);
            LayoutParams params = new LayoutParams();
            params.width=LayoutParams.MATCH_PARENT;
            params.height=LayoutParams.MATCH_PARENT;
            imgView.setLayoutParams(params);
            imgView.setBackgroundResource(R.color.gray);
            imgView.setScaleType(ImageView.ScaleType.FIT_XY);
            ImageLoadUtil.load(mContext,banner.getSrc(),imgView);
            list.add(imgView);
        }
    }

    /**
     * pagerAdapter
     */
    private class PagerAdapter extends android.support.v4.view.PagerAdapter implements OnClickListener {

        private boolean isInfiniteLoop;

        public PagerAdapter(){
            this(true);
        }

        private int lastPostion;

        public PagerAdapter(boolean isInfiniteLoop){
            this.isInfiniteLoop = isInfiniteLoop;
        }

        public boolean isInfiniteLoop() {
            return isInfiniteLoop;
        }

        public PagerAdapter setInfiniteLoop(boolean isInfiniteLoop) {
            this.isInfiniteLoop = isInfiniteLoop;
            return this;
        }

        @Override
        public int getCount() {
            return list==null?0:list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(list.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            View  view = list.get(position);
            view.setTag(position);
            view.setOnClickListener(this);

            if(view.getParent() != null)
                ((ViewGroup)view.getParent()).removeView(view);
            container.addView(view);
            return  view;
        }

        @Override
        public void onClick(View view) {
            int position = (int) view.getTag();
        }
    }
}

